package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/*
 *   Description LessonChangeListener
 *   @author zhangyan
 *   @time   2024/12/7
 */
@Slf4j
//引入依赖
@RequiredArgsConstructor
@Component
public class LessonChangeListener {

    private final ILearningLessonService learningLessonService;

    /**
     * 监听课程支付成功
     * @param order
     */
    @RabbitListener(
           bindings =@QueueBinding(
                   value = @Queue(value = "lessoning.pay.queue", durable = "true"),
                   exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE,type = "topic"),
                   key = MqConstants.Key.ORDER_PAY_KEY
           )
    )
    public void listenLessonPay(OrderBasicDTO order){
        //健壮性处理
        if(order == null || order.getUserId() == null || CollUtils.isEmpty(order.getCourseIds())){
            log.error("MQ消息的课程支付，异常消息，信息有空值");
            return;
        }
        //添加课程
        learningLessonService.addUserLessons(order.getUserId(), order.getCourseIds());

    }


}
